package cn.itlaobing.dao;

import cn.itlaobing.model.UsersModel;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.Date;

/**
 * Created by Administrator on 2017-11-15.
 */
public class RegisterDao extends BaseDao {
    public Boolean isRegister(String phone) throws SQLException {
        QueryRunner qr=new QueryRunner(getDataSource());
        String sql="SELECT * FROM users WHERE userAccount=?";
        BeanHandler<UsersModel> beanHandler=new BeanHandler<UsersModel>(UsersModel.class);
        UsersModel r=qr.query(sql,beanHandler,phone);
        if(r!=null)
        {
            return true;
        }
        return false;
    }
    public int Register(UsersModel usersModel) throws SQLException {
        QueryRunner qr=new QueryRunner(getDataSource());
        String sql="INSERT INTO users (userAccount,userPassword,userRegDateTime) VALUES(?,?,?) ";
        int r=qr.update(sql,usersModel.getUserAccount(),usersModel.getUserPassword(),usersModel.getUserRegDateTime());
        return r;
    }
    public UsersModel login(String username,String password) throws SQLException {
        QueryRunner qr=new QueryRunner(getDataSource());
        String sql="SELECT * FROM users WHERE userAccount=? AND userPassword= ?";
        BeanHandler<UsersModel> beanHandler=new BeanHandler<UsersModel>(UsersModel.class);
        return qr.query(sql,beanHandler,username,password);
    }
    public int updateLastTime(Integer count,Integer id) throws SQLException {
        QueryRunner qr=new QueryRunner(getDataSource());
        String sql="UPDATE users SET userLastLoginDateTime=?,userLoginCount=? WHERE userId=?";
        return qr.update(sql,new Date(),count,id);
    }
}
